package se.webbzon.boltzmann.editor.component;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

@Deprecated public class NullableJComboBox<T> extends JComboBox<T> {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 633214414411424971L;
	
	// Whatever null is selected or not
	private boolean nullSelected;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new nullable combo box with a combo box
	 * model. **/
	public NullableJComboBox(ComboBoxModel<T> model) {
		super(model);
		nullSelected = true;
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public T getItemAt(int index) {
		if (index == 0)
			return null;
		else
			return super.getItemAt(index);
	}

	@Override public int getItemCount() {
		return super.getItemCount();
	}

	@Override public Object[] getSelectedObjects() {
		if (nullSelected)
			return new Object[] {null};
		else
			return super.getSelectedObjects();
	}

	@Override public void insertItemAt(T item, int index) {
		if (index > 0)
			super.insertItemAt(item, index-1);
	}

	@Override public void removeItem(Object anObject) {
		super.removeItem(anObject);
	}

	@Override public void removeItemAt(int anIndex) {
		if (anIndex > 0)
			super.removeItemAt(anIndex-1);
	}

	@Override public int getSelectedIndex() {
		if (nullSelected)
			return 0;
		else
			return super.getSelectedIndex();
	}

	@Override public Object getSelectedItem() {
		if (nullSelected)
			return null;
		else
			return super.getSelectedItem();
	}

	@Override public void setSelectedIndex(int anIndex) {
		if (anIndex == 0)
			nullSelected = true;
		else
			nullSelected = false;
		
		super.setSelectedIndex(anIndex);
	}

	@Override public void setSelectedItem(Object anObject) {
		if (anObject == null)
			nullSelected = true;
		else 
			nullSelected = false;
		
		System.out.println(anObject);
		super.setSelectedItem(anObject);
	}	

}
